package com.auto.learning.activity;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.auto.learning.R;
import com.auto.learning.bean.BookBean;
import com.auto.learning.event.EventBus;
import com.auto.learning.fragment.CollectPageFragment;
import com.auto.learning.net.Cache;
import com.auto.learning.net.HttpRequestHelper;
import com.auto.learning.service.MusicPlayManager;
import com.auto.learning.view.SharedPreferencesUtils;
import com.squareup.otto.Subscribe;
import com.squareup.otto.ThreadEnforcer;

public class CollectReadActivity extends BaseActivity {

	private CollectPageFragment collectFragment;
	public FragmentManager fm;
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HttpRequestHelper.GET_LOCAL_BOOKBEAN_SUCCESS:
				final BookBean bean = (BookBean) msg.obj;
				if (bean != null) {
					handler.postDelayed(new Runnable() {
						public void run() {
							ArrayList<BookBean> m_list = new ArrayList<BookBean>();
							m_list.add(bean);
							MusicPlayManager.getInstance(CollectReadActivity.this).setPlayList(m_list).setBookId(bean.getBookId()).setContinuePlayPause(true).play();
						}
					}, 1500);
					Intent intent = new Intent(CollectReadActivity.this, PlayActivity.class);
					intent.putExtra("bookId", bean.getBookId());
					intent.putExtra("faceImg", TextUtils.isEmpty(bean.getFaceImg()) ? "" : bean.getFaceImg());
					intent.putExtra("canRead",bean.getCanRead());
					intent.putExtra("needBuy",bean.getNeedBuy());
					startActivity(intent);
				}
				break;
			}
		
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setViewInLine(R.layout.collect_read_activity);
		initView();
		registerEvent();
	}
	
	private void initView(){
		setTitle("已读书架");
		collectFragment = new CollectPageFragment();
		collectFragment.setType(2);
		fm = getSupportFragmentManager(); 
		FragmentTransaction transaction = fm.beginTransaction();
		transaction.replace(R.id.main_view, collectFragment);
		transaction.commit();
		
		backBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if (collectFragment.isNeedRefreshMain()) {
					setResult(662);
					finish();
				}else {
					finish();
				}
			}
		});
		
		
		if (MusicPlayManager.getInstance(this).isPlaying()) {
			animPlaying();
		}else {
			animPlayed();
		}
	}
	
	
	@Override
	protected void onDestroy() {
		unRegisterEvent();
		super.onDestroy();
	}
	
	public void registerEvent(){
		EventBus.getEventBus("m_play_stute" , ThreadEnforcer.MAIN).register(this);
	}
	
	
	public void unRegisterEvent(){
		EventBus.getEventBus("m_play_stute" , ThreadEnforcer.MAIN).unregister(this);
	}
	
	@Subscribe
	public void onEventPlayStute(Boolean isPlaying){
		collectFragment.notifyUI();
		onResume();
	}
}
